import pygtk
pygtk.require('2.0')

import gtk;
from gtk import gdk
import omg_dbus
import gobject
import omg_types
import locale

class TrophyHall(gtk.Window):

    def __init__ (self):
        gtk.Window.__init__(self)

        self.set_title("OMG Trophies")
        self.set_default_size (250, 100)
        view = gtk.TreeView()
        self.setup_treeview(view)
        self.add(view)
        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def setup_treeview(self, view):

        stock_icon = 'gtk-dialog-info'

        locale_str = locale.getdefaultlocale()[0]

        store = gtk.TreeStore(gobject.TYPE_STRING, gtk.gdk.Pixbuf,
                              gobject.TYPE_STRING, gtk.gdk.Pixbuf,
                              gobject.TYPE_STRING, gobject.TYPE_STRING,
                              gobject.TYPE_STRING
                              )

        view.set_model(store);

        view.insert_column_with_attributes(-1, "", gtk.CellRendererText(), text=0)

        column = gtk.TreeViewColumn()
        column.set_title("Set")
        view.append_column(column)

        renderer = gtk.CellRendererPixbuf()
        column.pack_start(renderer, expand=False)
        column.add_attribute(renderer, 'pixbuf', 1)

        renderer = gtk.CellRendererText()
        column.pack_start(renderer, expand=True)
        column.add_attribute(renderer, 'text', 2)

        view.insert_column_with_attributes(-1, "Icon", gtk.CellRendererPixbuf(), pixbuf=3)
        view.insert_column_with_attributes(-1, "Title", gtk.CellRendererText(), text=4)
        view.insert_column_with_attributes(-1, "Description", gtk.CellRendererText(), text=5)
        view.insert_column_with_attributes(-1, "Application", gtk.CellRendererText(), text=6)

        root = store.append(None)

        store.set_value(root, 0, "All Trophies");

        trophies_set = omg_dbus.getUnlockedTrophies();

        set_iter = store.append(root);

        for set_name in trophies_set.keys():

            store.set_value(set_iter, 2, set_name);

            trophies = trophies_set[set_name]
            trophy = None

            for trophy in trophies:

                trophy_iter = store.append(set_iter)

                trophy_icon_path = trophy.SetIcon

                try:
                    trophy_icon = gdk.pixbuf_new_from_file(set_icon_path)
                except:
                    if trophy.StockIcon:
                        stock_icon = trophy.StockIcon

                    trophy_icon = self.render_icon(stock_icon, gtk.ICON_SIZE_MENU)

                description = trophy.getDescription(locale_str)

                store.set_value(trophy_iter, 3, trophy_icon)
                store.set_value(trophy_iter, 4, trophy.Title)
                store.set_value(trophy_iter, 5, description)
                store.set_value(trophy_iter, 6, trophy.ApplicationFriendlyName)

            try:
                set_icon_path = trophy.SetIcon
                set_icon = gdk.pixbuf_new_from_file(set_icon_path)
            except:
                set_icon = self.render_icon(stock_icon, gtk.ICON_SIZE_MENU)

            store.set_value(set_iter, 1, set_icon)

        view.expand_all();
